<template>
  <div class="login">
    <div class="login-box">
      
      <el-form :model="ruleForm"  :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="名字" prop="username">
          <el-input  v-model="ruleForm.username" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input  v-model="ruleForm.password" ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login_req } from "@/api/login.js";
  export default {
    data(){
      return{
        ruleForm:{
          username:"",
          password:""
        },
        rules:{
          username:[
            { required:true, message:"请输入用户名",trigger:"blur"},
            { min:3, max:10, message:"长度在3到10字符",trigger:"blur"},
          ],   
          password:[
            { required:true, message:"请输入密码", trigger:"blue"},
            { min:3, max:8, message:"长度在3到8个字符",trigger:"blue"}
          ]
        }
        
      }
    },
    methods:{
      submitForm(ruleForm){
        console.log(ruleForm);
        this.$refs[ruleForm].validate(async ( validate ) =>{
          if(validate){
            const req = {
              method:"post",
              url:"login",
              data:this.ruleForm,
            };
            let result = await login_req(req);
            console.log(result);
            if(result.meta.status  == 200){
              console.log("跳转到主页");
              this.$router.push({
                path:"/home",
              });
              window.sessionStorage.setItem("token", result.data.token);
            }
          }
        })
      },
      resetForm(ruleForm){
        console.log(ruleForm);
      },
    }
  }
</script>

<style lang="scss" scoped>
.login{
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-box{
    width: 500px;
    height: 400px;
    border: 10px solid pink;
    border-radius: 10%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

</style>